package com.sx.pipeline.ui.mine

import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.sx.mvvmlibs.utils.Logs
import com.sx.pipeline.R
import com.sx.pipeline.repos.http.res.IdentityVO
import com.sx.pipeline.repos.http.res.MyUnitListVO
import org.w3c.dom.Text
import java.util.*

/**
 * @Author: YY
 * @Date: 2022/6/27
 * @Desc: TODO
 */
class SwitchBusinessAdapter(dataList: MutableList<MyUnitListVO>): BaseQuickAdapter<MyUnitListVO, BaseViewHolder>(
    R.layout.item_switch_business, dataList) {

    private var identityStack: MutableList<IdentitySelectAdapter> = mutableListOf()

    override fun convert(holder: BaseViewHolder, item: MyUnitListVO) {
        holder.setText(R.id.tv_unit_name, item.unitName)
        if (!item.isEnabled) {
            holder.getView<TextView>(R.id.tv_label).visibility = View.VISIBLE
        } else {
            holder.getView<TextView>(R.id.tv_label).visibility = View.GONE
        }

        val recyclerIdentity: RecyclerView = holder.getView(R.id.recycler_identity)
        recyclerIdentity.isNestedScrollingEnabled = false
        recyclerIdentity.layoutManager = LinearLayoutManager(
            context, LinearLayoutManager.VERTICAL, false
        )
        val identityListAdapter = IdentitySelectAdapter()
        identityListAdapter.data = item.identity as MutableList<IdentityVO>
        recyclerIdentity.adapter = identityListAdapter
        identityStack.add(identityListAdapter)

        identityListAdapter.setOnItemClickListener { adapter, view, position ->
            identityListAdapter.setSelectClickId(position)
            setOnItemClick(holder.getView(R.id.image_check), position)
            if (mOnNestChildClickListener != null) {
                mOnNestChildClickListener!!.onItemClickListener(item, holder.layoutPosition, position)
            }
        }

//        if (item.isExpand) {
//            holder.getView<LinearLayout>(R.id.line_list).visibility = View.VISIBLE
//        } else {
//            holder.getView<LinearLayout>(R.id.line_list).visibility = View.GONE
//        }
    }

    private var mOnNestChildClickListener: OnNestChildClickListener? = null

    interface OnNestChildClickListener {
        fun onItemClickListener(
            item: MyUnitListVO,
            parentPosition: Int,
            childPosition: Int
        )
    }

    fun setOnNestChildClickListener(onNestChildClickListener: OnNestChildClickListener) {
        mOnNestChildClickListener = onNestChildClickListener
    }

    fun removeSelection() {
        identityStack.forEach {
            it.clickId = -1
            it.notifyDataSetChanged()
        }
    }
}

class IdentitySelectAdapter: BaseQuickAdapter<IdentityVO, BaseViewHolder>(R.layout.item_identity_list) {

    var clickId: Int = -1

    override fun convert(holder: BaseViewHolder, item: IdentityVO) {
        when(item.identity) {
            "0" -> {
                holder.setImageResource(R.id.image_icon, R.drawable.icon_identity_operate)
                holder.setText(R.id.tv_identity, "运营单位")
            }
            "1" -> {
                holder.setImageResource(R.id.image_icon, R.drawable.icon_identity_build)
                holder.setText(R.id.tv_identity, "施工单位")
            }
            "2" -> {
                holder.setImageResource(R.id.image_icon, R.drawable.icon_identity_supply)
                holder.setText(R.id.tv_identity, "供应商")
            }
        }

        if (item.isEnabled) {
            Logs.d("IdentitySelectAdapter $clickId === ${holder.layoutPosition}")
            // 单选
            if (clickId == holder.layoutPosition) {
                holder.setImageResource(R.id.image_check, R.drawable.icon_radio_button_checked)
            } else {
                holder.setImageResource(R.id.image_check, R.drawable.icon_radio_button_dischecked)
            }
        } else {
            // 不可选择
            holder.setImageResource(R.id.image_check, R.drawable.icon_not_choice)
            holder.getView<ImageView>(R.id.image_check).isEnabled = false
        }
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setSelectClickId(position: Int) {
        this.clickId = position
        notifyDataSetChanged()
    }
}